<?php

class profileActions extends sfActions {
	
	public function executeIndex() {
		$user = $this->getUser()->getGuardUser();
		$this->user = $user;
		 if (!$this->getUser()->isAuthenticated()){
			$this->forward(sfConfig::get('sf_login_module'), sfConfig::get('sf_login_action'));
		}
		
		if($this->getRequest()->getMethod() == sfRequest::POST) {
			$oldPassword = $this->getRequestParameter('old_password');
			$newPassword = $this->getRequestParameter('new_password');
			$confirmPassword = $this->getRequestParameter('confirm_new_password');
			if($this->getUser()->checkPassword($oldPassword)) {
				if(empty($newPassword)) {
					$this->getRequest()->setError('new_password', 'Mật khẩu mới không được để trống');
				}elseif($newPassword != $confirmPassword) {
					$this->getRequest()->setError('confirm_new_password', 'Mật khẩu mới phải được xác nhận chính xác');
				}else { // OK, change pass
					$this->getUser()->setPassword($newPassword);
					$this->setFlash('notice', 'Mật khẩu của bạn đã được đổi thành công');
				}
			} else {
				$this->getRequest()->setError('old_password',
						'Mật khẩu cũ không chính xác');
			}
		}
	}
	
 	public function executeChangeUsername(){

        $username = trim($this->getRequestParameter('username', null));
        $newname = trim($this->getRequestParameter('newname', null));

        try {

            if (($username != null)&&($newname != null)) {
                $c = new Criteria();
                $c->add(UserPeer::USERNAME, $newname);
                $usernew = UserPeer::doSelectOne($c);
                if ($usernew) {
                    echo "ERROR: User having username '$newname' existed!<br/>";
                } else {
                    $c = new Criteria();
                    $c->add(UserPeer::USERNAME, $username);
                    $user = UserPeer::doSelectOne($c);
                    if ($user) {

                        //var_dump($user);exit();

                        $user->setUsername($newname);
                        $user->setNew(false);
                        $user->save();

                        //change ringtone's creator_name
                        $connection = Propel::getConnection();
                        $query = "UPDATE ringtone SET creator_name = '{$newname}'
                            WHERE creator_name = '{$username}'";
                        $statement = $connection->prepareStatement($query);
                        $resultset = $statement->executeQuery();                        

                        echo "Change username finished!<br/>";
                    } else {
                        echo "ERROR: User having username '$username' is not existed!<br/>";
                    }
                }
            } else {
                echo "ERROR: username or newname is missing!<br/>";
            }
        } catch (Exception $e) {
            echo "EXCEPTION: ".$e->getMessage();
        }

        exit();
    }	
}